package com.example.demo.service.impl;

import com.example.demo.core.RequestBuilder;
import com.example.demo.domain.dto.ChatDTO;
import com.example.demo.service.ChatService;
import org.springframework.core.ParameterizedTypeReference;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Optional;

/**
 * Python 聊天
 * @author kisofdeath
 * @date 2022/5/11 12:32
 */
@Service("pyChatService")
public class PyChatServiceImpl implements ChatService {

	/**
	 * RASA 机器人
	 * @param params
	 * @return
	 */
	@Override
	public String getChatRespText(ChatDTO params) {
		Map<String, String> p = new HashMap<>();
		p.put("sender", params.getUname());
		p.put("message", params.getContent());
		List<Map<String, String>> resp = new RequestBuilder()
				.data(p)
				.post("http://localhost:5005/webhooks/rest/webhook",
						new ParameterizedTypeReference<List<Map<String, String>>>() {
						});
		String result = null;
		if (resp != null && resp.size() > 0) {
			Map<String, String> res = resp.get(0);
			result = res.get("text");
		}
		return Optional.ofNullable(result).orElse("你说得对");
	}
}
